﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using Entities;
using Entities.IRepository;
using DAL;
namespace HeThongNhaSach
{
    public partial class TimKiemHD : DevComponents.DotNetBar.Office2007Form
    {
        public frmMain frmChinh = new frmMain();
        public TimKiemHD()
        {
            InitializeComponent();
        }

        private void TimKiemHD_Load(object sender, EventArgs e)
        {
            GeneralRepository<NhanVien> _NVRepo = new GeneralRepository<NhanVien>();
            List<NhanVien> lnv = _NVRepo.GetAll().ToList();
            foreach (NhanVien nv in lnv)
                cbbMaNV.Items.Add(nv.MaNhanVien);
            cbbMaNV.Enabled = false;
            grpTimTheoNgay.Enabled = false;
            txtTimMa.Enabled = false;
            btnTimKiem.Enabled = false;
        }
        /// <summary>
        /// tìm kiếm theo mã hóa đơn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbTimTheoMa_CheckedChanged(object sender, EventArgs e)
        {
             if (cbTimTheoMa.Checked)
            {
                txtTimMa.Enabled = true;
                btnTimKiem.Enabled = true;
            }
            else
            {
                txtTimMa.Enabled = false;
                if (cbTimTheoMa.Checked || cbTimTheoMaNV.Checked || cbTimTheoNgay.Checked ) ;
                else
                    btnTimKiem.Enabled = false;
            }
        }
        /// <summary>
        /// tìm kiếm theo mã nhân viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbTimTheoMaNV_CheckedChanged(object sender, EventArgs e)
        {
              if (cbTimTheoMaNV.Checked)
            {
                cbbMaNV.Enabled = true;
                btnTimKiem.Enabled = true;
            }
            else
            {
                cbbMaNV.Enabled = false;
                if (cbTimTheoMa.Checked || cbTimTheoMaNV.Checked || cbTimTheoNgay.Checked ) ;
                else
                    btnTimKiem.Enabled = false;
            }
        }
        /// <summary>
        /// tìm kiếm theo ngày lập hóa đơn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbTimTheoNgay_CheckedChanged(object sender, EventArgs e)
        {
             if (cbTimTheoNgay.Checked)
            {
                grpTimTheoNgay.Enabled = true;
                btnTimKiem.Enabled = true;
            }
            else
            {
                grpTimTheoNgay.Enabled = false;
                if (cbTimTheoMa.Checked || cbTimTheoMaNV.Checked || cbTimTheoNgay.Checked ) ;
                else
                    btnTimKiem.Enabled = false;
            }
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// tìm kiếm hóa đơn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            GeneralRepository<HoaDon> _HDRepo = new GeneralRepository<HoaDon>();
            List<HoaDon> lsb = _HDRepo.GetAll().ToList();
            if (cbTimTheoMa.Checked && !string.IsNullOrEmpty(txtTimMa.Text.Trim()))
            {
                lsb = lsb.FindAll(delegate(HoaDon s)
                {
                    return s.MaHoaDon.Contains(txtTimMa.Text.Trim());
                });
            }
            if (cbTimTheoMaNV.Checked)
            {
                lsb = lsb.FindAll(delegate(HoaDon s)
                {
                    return s.MaNhanVien == cbbMaNV.Text;
                });
            }
           
            if (cbTimTheoNgay.Checked)
            {
                try
                {
                    DateTime min = dtMin.Value;
                    DateTime max = dtMax.Value;
                    lsb = lsb.FindAll(delegate(HoaDon s)
                    {
                        return s.NgayLap >= min && s.NgayLap <= max;
                    });
                }
                catch
                {
                    MessageBox.Show("Nhập ngày cần tìm");
                    return;
                }
            }
            
            if (lsb.Count <= 0)
            {
                MessageBox.Show("Không tìm thấy hóa đơn phù hợp");
            }
            else
            {
                frmChinh.tabControl1.SelectedTab = frmChinh.tabHD;
                frmChinh.dgvHoaDon.DataSource = new List<HoaDon>();
                frmChinh.dgvHoaDon.DataSource = lsb;
                this.Close();
            }
        }
    }
}